package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.ResultReader;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.OperDesmonteRet;

public class OperDesmonteRetDAOImpl extends JdbcDaoSupport implements IOperDesmonteRetDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;

    private FindByActiveStateQuery findByActiveStateQuery;
    
    private SearchByParametersDinamycally searchByParametersDinamycally;

	private static final String SELECT = "SELECT * FROM OPE_DESMONTE_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(OperDesmonteRet operdesmonteret) {
		deleteOperation.execute( operdesmonteret  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public OperDesmonteRet findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(OperDesmonteRet operdesmonteret) {
		insertOperation.execute( operdesmonteret );
	}

	public void update(OperDesmonteRet operdesmonteret) {
		updateOperation.execute( operdesmonteret );
	}

	public OperDesmonteRet findByCriteria(OperDesmonteRet  operdesmonteret) {
		return findByCriteriaQuery.runQuery(operdesmonteret);
	}

	public List load(OperDesmonteRet operdesmonteret) {
		return findAllQuery.runQuery(operdesmonteret);
	}
	
	public List searchByParametersDinamycally(OperDesmonteRet operdesmonteret) {
		return searchByParametersDinamycally.runQuery(operdesmonteret);
	}

	public OperDesmonteRet findByActiveState(OperDesmonteRet  operdesmonteret) {
		return findByActiveStateQuery.runQuery(operdesmonteret);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
//    	deleteOperation = new DeleteOperation(  getDataSource() );
//    	deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
//    	findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
//    	findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
//    	findAllQuery = new FindAllQuery( getDataSource() );
		searchByParametersDinamycally = new SearchByParametersDinamycally( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO OPE_DESMONTE_T ("
			
				  + " ID, "
				  
				  + " CODIGO, "
				  
				  + " FECHA_REGISTRO, "
				  + " HORA, "
				  
				  + " NOMBRE_INFRACTOR, "
				  + " DOCUMENTO_INFRACTOR, "
				  
				  + " ID_TIPO_INFRACCION, "
				  + " ID_PERMISO_DEP, "
				  
				  + " DIRECCION_RETENCION, "
				  + " CODIGO_BARRIO, "
				  + " BARRIO, "
				  + " CODIGO_COMUNA, "
				  + " COMUNA, "
				  + " LUGAR, "
				  
				  + " ID_TIPO_VENTA, "
				  + " ID_TIPO_DESMONTE, "
				  
				  + " OBSERVACIONES, "
				  + " NOMBRE_RESPONSABLE, "
				  + " DOCUMENTO_RESPONSABLE, "
				  + " NOMBRE_ENCARGADO_BODEGA, "
				  + " DOCUMENTO_ENCARGADO_BODEGA, "
				  + " ESTADO, "
				  
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  + ") VALUES ("
				  
			  	  + " OPE_DESMONTE_SEC.nextVal, "
			  	  
				  + PARAMETER
				  
				  + PARAMETER
				  + PARAMETER
				  
				  + PARAMETER
				  + PARAMETER
				  
				  + PARAMETER
				  + PARAMETER
				  
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  
				  + PARAMETER
				  + PARAMETER
				  
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  
				  + PARAMETER
				  + "SYSDATE "
				  
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters

			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(OperDesmonteRet operdesmonteret) {
			InsertOperation.this.update( new Object[]{

				operdesmonteret.getCodigo(),
				
				operdesmonteret.getFechaRegistro(),
				operdesmonteret.getHora(),
				
				operdesmonteret.getNombreInfractor(),
				operdesmonteret.getDocumentoInfractor(),
				
				operdesmonteret.getIdTipoInfraccion(),
				operdesmonteret.getIdPermisoDep(),
				
				operdesmonteret.getDireccionRetencion(),
				operdesmonteret.getCodigoBarrio(),
				operdesmonteret.getBarrio(),
				operdesmonteret.getCodigoComuna(),
				operdesmonteret.getComuna(),
				operdesmonteret.getLugar(),
				
				operdesmonteret.getIdTipoVenta(),
				operdesmonteret.getIdTipoDesmonte(),
				
				operdesmonteret.getObservaciones(),
				operdesmonteret.getNombreResponsable(),
				operdesmonteret.getDocumentoResponsable(),
				operdesmonteret.getNombreEncargadoBodega(),
				operdesmonteret.getDocumentoEncargadoBodega(),
				operdesmonteret.getEstado(),
				
				operdesmonteret.getUsuarioCrea()
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE OPE_DESMONTE_T SET "
			  
					  + " CODIGO = ? , "
					  
					  + " FECHA_REGISTRO = ? , "
					  + " HORA = ? , "
					  
					  + " NOMBRE_INFRACTOR = ? , "
					  + " DOCUMENTO_INFRACTOR = ? , "
					  
					  + " ID_TIPO_INFRACCION = ? , "
					  + " ID_PERMISO_DEP = ? , "
					  
					  + " DIRECCION_RETENCION = ? , "
					  + " CODIGO_BARRIO = ? , "
					  + " BARRIO = ? , "
					  + " CODIGO_COMUNA = ? , "
					  + " COMUNA = ? , "
					  + " LUGAR = ? , "
					  
					  + " ID_TIPO_VENTA = ? , "
					  + " ID_TIPO_DESMONTE = ? , "
					  
					  + " OBSERVACIONES = ? , "
					  + " NOMBRE_RESPONSABLE = ? , "
					  + " DOCUMENTO_RESPONSABLE = ? , "
					  + " NOMBRE_ENCARGADO_BODEGA = ? , "
					  + " DOCUMENTO_ENCARGADO_BODEGA = ? , "
					  + " ESTADO = ? , "
					  
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE "
				  	  + WHERE
					  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			

			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.INTEGER));

			compile();
		}
		public void execute(OperDesmonteRet operdesmonteret) {
			UpdateOperation.this.update( new Object[]{
				operdesmonteret.getCodigo(),
				
				operdesmonteret.getFechaRegistro(),
				operdesmonteret.getHora(),
				
				operdesmonteret.getNombreInfractor(),
				operdesmonteret.getDocumentoInfractor(),
				
				operdesmonteret.getIdTipoInfraccion(),
				operdesmonteret.getIdPermisoDep(),
				
				operdesmonteret.getDireccionRetencion(),
				operdesmonteret.getCodigoBarrio(),
				operdesmonteret.getBarrio(),
				operdesmonteret.getCodigoComuna(),
				operdesmonteret.getComuna(),
				operdesmonteret.getLugar(),
				
				operdesmonteret.getIdTipoVenta(),
				operdesmonteret.getIdTipoDesmonte(),
				
				operdesmonteret.getObservaciones(),
				operdesmonteret.getNombreResponsable(),
				operdesmonteret.getDocumentoResponsable(),
				operdesmonteret.getNombreEncargadoBodega(),
				operdesmonteret.getDocumentoEncargadoBodega(),
				operdesmonteret.getEstado(),

				operdesmonteret.getUsuarioModifica(),
				
				operdesmonteret.getId()
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM OPE_DESMONTE_T "
										+ WHERE
			  	  	  					+ " CODIGO = ? ";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
		}
		public void execute(OperDesmonteRet operdesmonteret) {
			this.update( new Object[]{
				operdesmonteret.getCodigo(),
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM OPE_DESMONTE_T "
										+ WHERE;
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }
    
    
 // ------------- SearchByParametersDynamically ----------------------------------
    class SearchByParametersDinamycally extends BaseQueryOperation	{
    	
		private static final String sql = SELECT
										+ WHERE
										+ " 1=1 "
										+ " AND FECHA_REGISTRO BETWEEN  ? "
										+ " AND ? + INTERVAL '1' DAY "
										+ " AND ID_TIPO_DESMONTE = ? "
										+ " AND CODIGO LIKE ? ";
										
		
		public SearchByParametersDinamycally(DataSource ds) {
			super(ds,sql);
			declareParameter(new SqlParameter(Types.DATE));
			declareParameter(new SqlParameter(Types.DATE));
			declareParameter(new SqlParameter(Types.INTEGER));
			declareParameter(new SqlParameter(Types.CHAR));
			compile();
		}
		
		
		public List runQuery(OperDesmonteRet operdesmonteret) {
			List result =  execute(new Object[]{
					operdesmonteret.getFechaInicio(),
					operdesmonteret.getFechaFin(),
					operdesmonteret.getIdTipoDesmonte(),
					new String(operdesmonteret.getCodigo()!=null&&!operdesmonteret.getCodigo().equals("")? operdesmonteret.getCodigo():"%")
			});
			
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
		}
	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			OperDesmonteRet operdesmonteret = new OperDesmonteRet();
				operdesmonteret.setId(new Long(rs.getLong("ID")));
				operdesmonteret.setCodigo(rs.getString("CODIGO"));
				operdesmonteret.setFechaRegistro(rs.getDate("FECHA_REGISTRO"));		
				operdesmonteret.setHora(rs.getString("HORA"));
				operdesmonteret.setNombreInfractor(rs.getString("NOMBRE_INFRACTOR"));
				operdesmonteret.setDocumentoInfractor(rs.getString("DOCUMENTO_INFRACTOR"));
				operdesmonteret.setIdTipoInfraccion(new Long(rs.getLong("ID_TIPO_INFRACCION")));
				operdesmonteret.setIdPermisoDep(new Long(rs.getLong("ID_PERMISO_DEP")));
				operdesmonteret.setDireccionRetencion(rs.getString("DIRECCION_RETENCION"));
				operdesmonteret.setCodigoBarrio(rs.getString("CODIGO_BARRIO"));
				operdesmonteret.setBarrio(rs.getString("BARRIO"));
				operdesmonteret.setCodigoComuna(rs.getString("CODIGO_COMUNA"));
				operdesmonteret.setComuna(rs.getString("COMUNA"));
				operdesmonteret.setLugar(rs.getString("LUGAR"));
				operdesmonteret.setIdTipoVenta(new Long(rs.getLong("ID_TIPO_VENTA")));
				operdesmonteret.setIdTipoDesmonte(new Long(rs.getLong("ID_TIPO_DESMONTE")));
				operdesmonteret.setObservaciones(rs.getString("OBSERVACIONES"));
				operdesmonteret.setNombreResponsable(rs.getString("NOMBRE_RESPONSABLE"));
				operdesmonteret.setDocumentoResponsable(rs.getString("DOCUMENTO_RESPONSABLE"));
				operdesmonteret.setNombreEncargadoBodega(rs.getString("NOMBRE_ENCARGADO_BODEGA"));
				operdesmonteret.setDocumentoEncargadoBodega(rs.getString("DOCUMENTO_ENCARGADO_BODEGA"));
				operdesmonteret.setEstado(rs.getString("ESTADO"));
				operdesmonteret.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				operdesmonteret.setFechaCrea(rs.getDate("FECHA_CREA"));		
				operdesmonteret.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				operdesmonteret.setFechaModifica(rs.getDate("FECHA_MODIFICA"));		
			return operdesmonteret;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE;
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public OperDesmonteRet runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (OperDesmonteRet) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND ID = ? "
			  	  	  					//+ " AND ID_OPER_DESMONTE = ? "
			  	  	  					//+ " AND CODIGO = ? "
			  	  	  					//+ " AND FECHA_REGISTRO = ? "
			  	  	  					//+ " AND HORA = ? "
			  	  	  					//+ " AND NOMBRE_INFRACTOR = ? "
			  	  	  					//+ " AND DOCUMENTO_INFRACTOR = ? "
			  	  	  					//+ " AND ID_TIPO_INFRACCION = ? "
			  	  	  					//+ " AND ID_PERMISO_DEP = ? "
			  	  	  					//+ " AND DIRECCION_RETENCION = ? "
			  	  	  					//+ " AND LUGAR = ? "
			  	  	  					//+ " AND ID_TIPO_VENTA = ? "
			  	  	  					//+ " AND OBSERVACIONES = ? "
			  	  	  					//+ " AND NOMBRE_RESPONSABLE = ? "
			  	  	  					//+ " AND DOCUMENTO_RESPONSABLE = ? "
			  	  	  					//+ " AND NOMBRE_ENCARGADO_BODEGA = ? "
			  	  	  					//+ " AND DOCUMENTO_ENCARGADO_BODEGA = ? "
			  	  	  					//+ " AND ESTADO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public OperDesmonteRet runQuery(OperDesmonteRet operdesmonteret) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//operdesmonteret.getId(),
				//operdesmonteret.getIdOperDesmonte(),
				//operdesmonteret.getCodigo(),
				//operdesmonteret.getFechaRegistro(),
				//operdesmonteret.getHora(),
				//operdesmonteret.getNombreInfractor(),
				//operdesmonteret.getDocumentoInfractor(),
				//operdesmonteret.getIdTipoInfraccion(),
				//operdesmonteret.getIdPermisoDep(),
				//operdesmonteret.getDireccionRetencion(),
				//operdesmonteret.getLugar(),
				//operdesmonteret.getIdTipoVenta(),
				//operdesmonteret.getObservaciones(),
				//operdesmonteret.getNombreResponsable(),
				//operdesmonteret.getDocumentoResponsable(),
				//operdesmonteret.getNombreEncargadoBodega(),
				//operdesmonteret.getDocumentoEncargadoBodega(),
				//operdesmonteret.getEstado(),
				//operdesmonteret.getUsuarioCrea(),
				//operdesmonteret.getFechaCrea(),
				//operdesmonteret.getUsuarioModifica(),
				//operdesmonteret.getFechaModifica(),
				});
			if ( result.size() > 0 ) {
        		return (OperDesmonteRet) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery(OperDesmonteRet operdesmonteret) {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE;
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public OperDesmonteRet runQuery(OperDesmonteRet operdesmonteret) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (OperDesmonteRet) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }


}
